<template>
  <a-card :bordered="false">
    <!--<a-row :gutter="8">
      <a-col :span="5">
        <a-tree
          @expand="onExpand"
          :expandedKeys="expandedKeys"
          :autoExpandParent="autoExpandParent"
          @select="onSelect"
          :selectedKeys="selectedKeys"
          :treeData="storeTreeDate"
        />
      </a-col>
      <a-col :span="19">-->
    <div class="table-page-search-wrapper" @keyup.enter="handleEnter">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="关键字">
              <a-input v-model="queryParam.keyword" placeholder="请输入名称/编码"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="选择等级">
              <a-select v-model="queryParam.level" placeholder="请选择">
                <a-select-option
                  v-for="(label,value) in levelMap"
                  :key="value"
                  :label="label"
                  :value="parseInt(value)">{{ label }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!--          <a-col :md="6" :sm="24">
            <a-form-item label="选择类型">
              <a-select v-model="queryParam.type" placeholder="请选择">
                <a-select-option
                  v-for="(label,value) in typeMap"
                  :key="value"
                  :label="label"
                  :value="parseInt(value)">{{ label }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>-->
          <a-col :md="6 || 24" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="handleOk">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button v-if="$auth('store-stores-add')" type="primary" icon="plus" @click="$refs.baseModal.base()">新增</a-button>
      <!--      <a-button style="margin-left: 8px" v-if="$auth('store-stores-export')" type="primary" icon="download" @click="doExport">导出</a-button>-->
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0 && $auth('store-stores-del')">
        <a-menu slot="overlay">
          <a-popconfirm title="删除仓库，将清空仓库所有物资数据？" @confirm="batchDelete()">
            <a-menu-item key="1"><a-icon type="delete" /><a>删除</a></a-menu-item>
          </a-popconfirm>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <a-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data-source="dataSource"
    >
      <span slot="action" slot-scope="record">
        <template>
          <a @click="handleView(record)">查看</a>
          <a-divider v-if="$auth('store-stores-del')" type="vertical" />
          <a v-if="$auth('store-stores-edit')" @click="handleEdit(record)">修改</a>
          <a-divider v-if="$auth('store-stores-del')" type="vertical" />
          <a-popconfirm v-if="$auth('store-stores-del')" title="删除仓库，将清空仓库所有物资数据？？" @confirm="batchDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a @click="changeStore(record)">迁移仓库</a>
          <!-- <a-divider type="vertical" />
           <a @click="doExportSpareStore(record)">导出库存</a>
           <a-divider type="vertical" />
           <a @click="doImport(record)">导入库存</a>
           <a-divider type="vertical" />
           <a @click="handleCopy(record)">复制</a>-->
        </template>
      </span>
      <span slot="delFlag" slot-scope="text">
        <badge
          :status="DictCache.COLOR.DELFLAG[text]"
          :text="delFlagMap[text]" />
      </span>
    </a-table>
    <!--</a-col>
    </a-row>-->
    <base-form ref="baseModal" @ok="handleOk"/>
    <detail ref="detailModal"/>
    <import-form-add ref="importModal" @ok="handleOk"/>
    <change-form ref="changeModal" @ok="handleOk"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import BaseForm from './modules/BaseForm'
import Detail from './modules/Detail'
import { fetchStoreTableTree, deleteStores, fetchStore, exportStore } from '@/api/store/store'
import ImportFormAdd from './modules/ImportFormAdd'
import { exportSpareStore } from '@/api/store/sparestore'
import ChangeForm from './modules/ChangeForm'

export default {
  name: 'StoreList',
  components: {
    STable,
    Ellipsis,
    BaseForm,
    Detail,
    ImportFormAdd,
    ChangeForm
  },
  props: {
    filter: {
      type: Number,
      default: -1
    }
  },
  data () {
    return {
      // 查询参数
      queryParam: {
        filter: this.filter
      },
      dataSource: [],
      expandedKeys: [],
      autoExpandParent: true,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return index + 1
          }
        },
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '等级',
          dataIndex: 'level',
          customRender: (text, record, index) => {
            return this.BaseTool.Object.getField(this.levelMap, text)
          }
        },
        {
          title: '排序',
          dataIndex: 'sort'
        },
        {
          title: '操作',
          key: 'action',
          width: '250px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 下拉框map
      typeMap: {},
      levelMap: {},
      companyList: {},
      projectList: {},
      delFlagMap: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          dataScope: {
          }
        }
        return fetchStoreTableTree(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    // 下拉框map
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.STORE_LEVEL)
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.STORE_TYPE)
    this.delFlagMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.DELFLAG)
    this.tableOption()
  },
  methods: {
    tableOption () {
      fetchStoreTableTree()
        .then(res => {
          this.dataSource = res.data
        })
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: false,
                name: record.id
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    batchDelete (id) {
      let ids = []
      if (this.BaseTool.String.isBlank(id)) {
        const length = this.selectedRows.length
        if (length === 0) {
          this.$message.info('请选择要删除的记录')
          return
        }
        ids = this.selectedRows.map(item => item.id)
      } else {
        ids = [id]
      }
      deleteStores(ids).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
        this.$refs.table.clearSelected()
      })
    },
    handleEdit (record) {
      fetchStore({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        modal.base(res.data)
      })
    },
    handleView (record) {
      fetchStore({ id: record.id }).then(res => {
        const modal = this.$refs.detailModal
        res.data.parentName = record.parentName
        res.data.userName = record.userName
        modal.base(res.data)
      })
    },
    doImport (record) {
      this.$refs.importModal.base(record.id)
    },
    changeStore (record) {
      this.$refs.changeModal.base(record)
    },
    handleOk () {
      fetchStoreTableTree()
        .then(res => {
          this.dataSource = res.data
        })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
        filter: this.filter
      }
      this.handleOk()
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      exportStore(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleCopy (record) {
      fetchStore({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        res.data.id = null
        modal.base(res.data)
      })
    },
    handleEnter () {
      this.handleOk()
    },
    doExportSpareStore (record) {
      if (record == null) {
        this.$message.error('请先选择仓库')
        return
      }
      exportSpareStore({ storeId: record.id }).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    }
  }
}
</script>
